package com.jfs.qy;

import com.jfs.qy.chain.AbstractServerStartHandler;
import com.jfs.qy.chain.component.LoginComponent;
import com.jfs.qy.config.DirectRabbitConfig;
import com.jfs.qy.enums.NvrApiEnum;
import com.jfs.qy.properties.NvrProperties;
import com.jfs.qy.service.LogoutService;
import com.jfs.qy.session.NvrSessionStorage;
import com.jfs.qy.utils.SystemCommonUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 华为nvr800api对接启动类
 */
@SpringBootApplication
/*@EnableAsync*/
@EnableConfigurationProperties(value = {NvrProperties.class})
public class HuaweiNvrApplication implements ApplicationRunner, ApplicationListener<ContextClosedEvent> {
    private final RestTemplate restTemplate;
    private final SystemCommonUtils systemCommonUtils;

    private final AbstractServerStartHandler abstractServerStartHandler;

    public HuaweiNvrApplication(RestTemplate restTemplate,
                                SystemCommonUtils systemCommonUtils,
                                @Qualifier(value = LoginComponent.BEAN_NAME)
                                AbstractServerStartHandler abstractServerStartHandler) {
        this.restTemplate = restTemplate;
        this.systemCommonUtils = systemCommonUtils;
        this.abstractServerStartHandler = abstractServerStartHandler;
    }

    public static void main(String[] args) {
        SpringApplication.run(HuaweiNvrApplication.class, args);
    }


    /**
     * spring 容器加载完后回调执行方法
     *
     * @param args
     * @throws Exception
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        //开始执行整个api调用链路 所有业务逻辑的入口
        abstractServerStartHandler.handlerOperation();
    }

    /**
     * spring容器销毁时回调方法
     *
     * @param event
     */
    @Override
    public void onApplicationEvent(ContextClosedEvent event) {
        //退出登录接口实现类
        LogoutService logoutService = () -> {
            final HttpEntity<?> entity = new HttpEntity<>(null, NvrSessionStorage.headers);
            String requestUrl = systemCommonUtils.getRequestUrl(NvrApiEnum.LOGOUT);
            return restTemplate.exchange(requestUrl, HttpMethod.POST, entity, String.class);
        };
        //调用退出登录
        logoutService.logout();
    }
}
